package com.highspeed.bdk.gateway.socket.analyze.message.gui.resolver;

import com.highspeed.bdk.gateway.Util;
import com.highspeed.bdk.gateway.socket.analyze.message.gui.base.MessageResolver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 3.6 初始化请求的反馈包
 * typedef struct initFbData
 * {
 * U8  reserved: 6,
 * dataVersion: 2; //01小网络模式 ,10大网络模式, 11保留
 * U8 resvered1;
 * U16 resvered2;
 * <p>
 * U32 boardSum; //板总数
 * U32 deviceNodeSum;// device nodes总数
 * U32 phyLinkSum; //物理链路总数
 * U32 phyPortSum; //物理端口总数
 * <p>
 * char data[0];//to save deviceNode,phylink,phyport,board
 * } InitFbData;
 */
public class InitFbDataResolver0x02 extends MessageResolver {

    @Override
    protected Map doResolveBody(byte[] response) {
        Map result = new HashMap();
        byte u8 = Util.getByte(response, getOffset());
        offset1();
        logger.info("U8字节数............" + u8);
        result.put("u8", u8);

        byte resevred1 = Util.getByte(response, getOffset());
        offset1();
        logger.info("resvered1.........." + resevred1);

        short resevred2 = Util.getShort(response, getOffset());
        offset2();
        logger.info("resevred2.........." + resevred2);

        int boardSum = Util.getInt(response, getOffset());
        offset4();
        logger.info("板卡数量........." + boardSum);
        result.put("boardSum", boardSum);

        int deviceNodeSum = Util.getInt(response, getOffset());
        offset4();
        logger.info("芯片节点数量........." + deviceNodeSum);
        result.put("deviceNodeSum", deviceNodeSum);

        int phyLinkSum = Util.getInt(response, getOffset());
        offset4();
        logger.info("物理链路数量........." + phyLinkSum);
        result.put("phyLinkSum", phyLinkSum);

        int phyPortSum = Util.getInt(response, getOffset());
        offset4();
        logger.info("物理端口数量........." + phyPortSum);
        result.put("phyPortSum", phyPortSum);

        //解析芯片节点
        DeviceNodeFbDataResolver0x03 deviceNodeFbDataResolver = new DeviceNodeFbDataResolver0x03();
        deviceNodeFbDataResolver.setContainer(getContainer());
        Map<String, List<Map>> cpus = new HashMap();
        for (int deviceSize = 0; deviceSize < deviceNodeSum; deviceSize++) {

            //网关原因，需要跳过两个字节
            offset2();
            logger.info("解析第" + deviceSize + "个芯片节点............");
            deviceNodeFbDataResolver.doResolveBody(response);
            Map cpu = (Map) getContainer().get("cpu");
            if (cpus.get("" + cpu.get("slotNum")) == null) {
                List<Map> map = new ArrayList<>();
                map.add(cpu);
                cpus.put("" + cpu.get("slotNum"), map);
            } else {
                cpus.get("" + cpu.get("slotNum")).add(cpu);
            }
        }

        //解析物理链路
        PhysicalLinkResolver physicalLinkResolver = new PhysicalLinkResolver();
        physicalLinkResolver.setContainer(getContainer());
        List<Map> phyLinks = new ArrayList<>();
        for (int i = 0; i < phyLinkSum; i++) {

            //网关原因，需要跳过两个字节
            offset2();
            logger.info("解析第" + i + "个物理链路.................");
            physicalLinkResolver.doResolveBody(response);
            phyLinks.add((Map) get("phyLink"));
        }

        //解析物理端口
        PhysicalPortResolver0x05 physicalPortResolver = new PhysicalPortResolver0x05();
        physicalPortResolver.setContainer(getContainer());
        List<Map> phyPorts = new ArrayList<>();
        for (int physicalPortSize = 0; physicalPortSize < phyPortSum; physicalPortSize++) {

            //网关原因，需要跳过两个字节
            offset2();
            logger.info("解析第" + physicalPortSize + "个物理端口.................");
            physicalPortResolver.doResolveBody(response);
            phyPorts.add((Map) get("phyPort"));
        }

        //解析板卡
        BoardFbDataResolver0x01 boardFbDataResolver = new BoardFbDataResolver0x01();
        boardFbDataResolver.setContainer(getContainer());
        List<Map> boards = new ArrayList<>();
        for (int boardSize = 0; boardSize < boardSum; boardSize++) {

            //网关原因，需要跳过两个字节
            offset2();
            logger.info("解析第" + boardSize + "个板卡.................");
            boardFbDataResolver.doResolveBody(response);
            Map board = (Map) get("board");
            boards.add(board);
            board.put("cpus", cpus.get(board.get("slotNum") + ""));
        }


        Map map = new HashMap();
        map.put("phyLinks", phyLinks);
        map.put("boards", boards);
        map.put("phyPorts", phyPorts);
        map.put("u8", u8);
        map.put("phyLinkSum", phyLinkSum);
        map.put("boardSum", boardSum);
        map.put("deviceNodeSum", deviceNodeSum);
        map.put("phyPortSum", phyPortSum);
        List machineBoxes = new ArrayList();
        Map machineBox = new HashMap();
        machineBox.put("machineBoxName", "超级机箱");
        machineBox.put("machineBoxNo", 0);
        machineBox.put("id", 0);
        machineBoxes.add(machineBox);
        map.put("machineBoxes", machineBoxes);
        put("result", map);
        return getContainer();
    }
}
